<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {

	public function _initialize()
    {
        //未登录、登录超时、长时间未动
    	if (empty($_SESSION['uid']) || time() > $_SESSION['overtime'] || empty($_SESSION['overtime']) ) {
            
    		$this->redirect('Common/login');
    	}

        session('overtime',(time()+60*60*C('overtime')));

        // // 用户信息
        // $uid = $_SESSION['uid'];

        // $m_user = M('User');
        // $where['id'] = $uid;

        // $userInfo = $m_user->where($where)->find();

        // $this->assign('userInfo',$userInfo);
        
    }


    /**
     * [index 首页]
     * @return [type] [description]
     */
    public function index(){
        $uid = $_SESSION['uid'];
        
        $m_user = M('User');

        $where['id'] = $uid;
        $userInfo = $m_user->where($where)->find();

        

        $this->assign('userInfo',$userInfo);
    	$this->display();
    }


    /**
     * [main 主页面]
     * @return [type] [description]
     */
    public function main(){
        $uid = $_SESSION['uid'];
        $m_news = M('News');
        $m_read = M('Read');
        $m_user = M('User');
        $m_applytask = M('Applytask');

        $registerScore = C('registerScore');
        $activateScore = C('activateScore');

        //新闻公告
        $where_top['top'] = 1;
        $topNews = $m_news->where($where_top)->order('id desc')->find();
        if ($topNews['id']) {//存在置顶
            $limit = 4;
        }else{  
            $limit = 5;
        }

        $where_no['top'] = 0;
        $newsList = $m_news->where($where_no)->order('id desc')->limit($limit)->select();

        //消息推送
        $where_read['status'] = 0;//未阅读
        $where_read['u_id'] = $uid;
        $noread_ids = $m_read->where($where_read)->field('n_id')->select();
        $n_ids = array_column($noread_ids, 'n_id');

        // 申请任务（is_apply   0：不可申请 1：可申请）
        // 不可申请：冻结、未激活、秘钥<5、任务积分<600、个人信息不完善、当天赢过一把、正在进行中的任务>=1
        
        $where_user['id'] = $uid;
        $userInfo = $m_user->where($where_user)->find();
        $userInfo['score_task'] = intval($userInfo['score_task']);
        $userInfo['score_trading'] = intval($userInfo['score_trading']);
        $userInfo['score_center'] = intval($userInfo['score_center']);
        $userInfo['key_task'] = intval($userInfo['key_task']);

        // if (empty($userInfo['phone']) || empty($userInfo['truename']) || (empty($userInfo['bankcard']) && empty($userInfo['wx_url']) && empty($userInfo['alipay_url'])) ) {
        //     $is_apply = 0;
        //     $msg = '个人信息不完善';

        //     $this->redirect('Personal/info');
        // }elseif ($userInfo['status'] == 2  ) {
        //     $is_apply = 0;
        //     $msg = '账号被冻结';

        // }elseif ($userInfo['status'] == 0  ) {
        //     $is_apply = 0;
        //     $msg = '账号未激活';
        // }elseif ($userInfo['key_task'] < 5 || $userInfo['score_task'] < $registerScore ) {
        //     $is_apply = 0;
        //     $msg = '任务秘钥小于5或任务积分小于'.$registerScore;
        // }else{
        //     //是否存在正在进行中的任务
        //     $where_applytask['user_id'] = $uid;
        //     $where_applytask['status'] = 0;
        //     $hadTask = $m_applytask->where($where_applytask)->find();
        //     if ($hadTask['id']) {
        //         $is_apply = 0;
        //         $msg = '您有未完成的任务，无法再次申请';
        //     }else{
        //         // 当天是否已经赢过一次
        //         $starttime = strtotime(date("Y-m-d 00:00:00"));
        //         $endtime = strtotime(date("Y-m-d 23:59:59"));

        //         $where_win['user_id'] = $uid;
        //         $where_win['winlose'] = 1;
        //         $where_win['createtime'] = array('between',array($starttime,$endtime));
        //         $winTask = $m_applytask->where($where_win)->find();
        //         if ($winTask['id']) {
        //             $is_apply = 0;
        //             $msg = '今天任务已完成，无法再次申请任务';
        //         }else{
        //             $is_apply = 1;
        //             $msg = '';
        //         }
        //     }
        // }

        // $this->assign('is_apply',$is_apply);
        // $this->assign('msg',$msg);
        $this->assign('topNews',$topNews);
        $this->assign('newsList',$newsList);
        $this->assign('n_ids',json_encode($n_ids));
        $this->assign('userInfo',$userInfo);
        $this->assign('activateScore',$activateScore);
        $this->display();
    }


    /**
     * [morePush 多条未读公告]
     * @return [type] [description]
     */
    public function morePush(){
        $ids = $_REQUEST['ids'];

        $m_news = M('News');

        $where['id'] = array('in',$ids);
        $newsList = $m_news->where($where)->order('id desc')->select();

        $this->assign('newsList',$newsList);
        $this->display();
    }

    /**
     * [ctrl_IsRead 标记为已阅读]
     * @return [type] [description]
     */
    public function ctrl_IsRead(){
        if(IS_AJAX){
            $uid = $_SESSION['uid'];

            $id = $_REQUEST['id'];
            $str_id = implode(',', $id);
            if (count($id) > 1) {
                $where['n_id'] = array('in',$str_id);
            }else{
                $where['n_id'] = $str_id;
            }
            
            $m_read = M('Read');

            $where['u_id'] = $uid;
            $save['status'] = 1;

            $res = $m_read->where($where)->save($save);
            if ($res) {
                $this->ajaxReturn(array(
                    'code'=>1,
                    'msg' => '修改状态成功'
                ));
            }else{
                $this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '修改状态失败'
                ));
            }

        }else{
            $this->redirect('Common/BadRequest');
        }
    }


    /**
     * [recreation 投注积分页面]
     * @return [type] [description]
     */
    public function recreation(){
        $uid = $_SESSION['uid'];

        $m_user = M('User');
        $m_applytask = M("Applytask");

        $where_user['id'] = $uid;
        $userInfo = $m_user->where($where_user)->find();

        $where_apply['user_id'] = $uid;
        $where_apply['status'] = 0;
        $applytaskInfo = $m_applytask->where($where_apply)->order('id desc')->find();
        if (empty($applytaskInfo['id'])) {
            $userInfo['score_recreation'] = 0;
        }else{
            if (empty($applytaskInfo['user_guess'])) {
                $userInfo['score_recreation'] = $applytaskInfo['num'];
            }else{
                $userInfo['score_recreation'] = 0;
            }
        }

        $this->assign('userInfo',$userInfo);
        $this->assign('applytaskInfo',$applytaskInfo);
        $this->display();
    }
    

}